<?php
namespace app\index\logic;
use app\common\model\Base;

class KjjgList extends Base
{
    //首页近期开奖列表
    static function getRunLotteryRecent() {
        $res = model('KjjgList')->getRunLotteryRecent();
		foreach($res as &$v) {
		    $v['jztime'] = date('Y-m-d',$v['jztime']);
			$kjjg = kjjg_split($v['kjjg'],$v['czid']);
			$v['kjjg_pic'] = $kjjg;
		}
		return $res;
    }
    
    //根据日期查询开奖信息
    public function getRunLotteryWithDay($czid,$timebh,$sql_shert) {
        if($timebh) {
            $sher_time = strtotime(date("Y-m-d")." 00:00:00")-24*3600*($timebh-1);
            $sher_time_o = strtotime(date("Y-m-d")." 23:59:59")-24*3600*($timebh-1);
            $sql_shert = $sql_shert." AND kl.jztime>{$sher_time} AND kl.jztime<{$sher_time_o}";
        }
        //根据条件选择所有彩种的开奖还是单独的开奖
        if(!$sql_shert) {
            $kjjg_list = model('KjjgList')->getLotteryNotices($czid);
        } else {
            $kjjg_list = model('KjjgList')->getLotteryNotice($czid,$sql_shert);
        }
        $fptime = KjjgList::name('caipai_list')->where('id',$czid)->value('cp_fp_time');
        //结果转为颜色球
        foreach($kjjg_list as &$v) {
            $v['jztime'] = date('Y-m-d H:i:s',$v['jztime']+$fptime);
            $v['kjjg'] = kjjg_split($v['kjjg'],$czid);
            //六合彩标记颜色
            if($czid == 9) {
                $v['kjjg_color'] = lhcTrans($v['kjjg']);
            }
        }
        return $kjjg_list;
    }
    
    //生成走势图表
    public function getTrendJson($czid,$hmq) {
        $boutnt_array = array();
        $yil_array = array();
        $weibu_s = '';
        $json_oneeye = '';
        $count_y = 0;
        $openkijjs = 0;
        
        $hq_kjshudb = KjjgList::name('kjjg_list')->where('czid',$czid)->where(' kjjg','!=','0')->order('jztime','DESC')->limit(30)->select()->toArray();
        foreach($hq_kjshudb as $zs) {
            //数据计算开始
            $count_y += 1; //数组总数  前台js循环使用
            //每一期数据计算开始
            $hmweishu = hmq_qg($zs['kjjg'],$zs['czid']);
            $kjjg = '000000';
            $json_qishu = '{"sGamePeriod":"';
            $json_qishu = $json_qishu.$zs['qihao'];
            $json_qishu = $json_qishu.'","sGamePeriods":"';
            $json_qishu = $json_qishu.$kjjg;
            $json_qishu = $json_qishu.'","iOpenNum":';
            $json_qishu = $json_qishu.$hmweishu[$hmq];
            //遗漏计算
            if($czid==1 || $czid==5 || $czid==7 || $czid==8 || $czid==10 || $czid==51 || $czid==62 || $czid==2) {
                $longchangdu = 10;
                $openkijjs = 0;
            }
            if($czid==4 || $czid==61 || $czid==63) {
                $longchangdu = 11;
                $openkijjs = 1;
            }
            if($czid==36 || $czid==44 || $czid==48 || $czid==72 || $czid == 39 || $czid == 40 || $czid == 41 || $czid == 43 || $czid == 46 || $czid == 47){
                $longchangdu = 7;
                $openkijjs = 1;
            }
            if($czid==16 || $czid==25 || $czid==28 || $czid==29){
                $longchangdu = 12;
                $openkijjs = 1;
            }
            if($czid==9||$czid==73) {
                $longchangdu = 51;
                $openkijjs = 1;
            }
            for($i=$openkijjs;$i<$longchangdu;$i++) {
                if(!isset($yil_array[$i])) $yil_array[$i] = 0;
                if($hmweishu[$hmq] == $i) {
                    $yil_array[$i] = 0;
                } else {
                    $yil_array[$i] = $yil_array[$i]+1;
                }
        
                if(!isset($boutnt_array[$i])) $boutnt_array[$i] = 0;
                if($yil_array[$i]>$boutnt_array[$i]) {
                    $boutnt_array[$i] = $yil_array[$i];
                } else {
                    $boutnt_array[$i] = 0;
                }
                $json_qishu = $json_qishu.',"iYL'.$i.'":';
                $json_qishu = $json_qishu.$yil_array[$i];
            }
        
            $json_qishu = $json_qishu.'}';
            if($json_oneeye == '') {
                $json_oneeye = $json_qishu;
            } else {
                $json_oneeye = $json_oneeye.",".$json_qishu;
            }
            //计算结束
        }
        
        //生成头部
        $josn_db_top = '{"RecordCount":'.$count_y.',"Records":[';
        //生成尾部
        //最大遗漏处理
        for($z=$openkijjs;$z<$longchangdu;$z++) {
            $weibu_s = $weibu_s.',"iYLMax'.$z.'":'.$boutnt_array[$z];
            $weibu_s = $weibu_s.',"iYLAvg'.$z.'":'.$boutnt_array[$z];
        }
        $weibu = ']'.$weibu_s.',"Func":"BaseTrend09","Result":1,"Desc":"OK"}';

        return $josn_db_top.$json_oneeye.$weibu;
    }
    
}